package models;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by yangxianming on 2019/10/29.
 */
public class GetRequestInputStream {
    public static JSONObject getJson(HttpServletRequest request){
        return JSON.parseObject(getString(request)) ;
    }

    public static String getString(HttpServletRequest request){
        //读取请求json
        InputStreamReader inputStreamReader;
        BufferedReader bufferedReader;
        String requestInfo = "";

        try {
            inputStreamReader = new InputStreamReader(request.getInputStream(), "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null)
                requestInfo = requestInfo + line;
        }catch (Exception e){
            //请求流如果出现异常的时候
            e.printStackTrace();
        }
        return requestInfo;
    }
}